﻿
Public Class TimeGroupList
    Private mWeekTimeGroup As List(Of WeekTimeGroup)


    Public Sub New()
        Dim i As Integer
        Dim oList(63) As WeekTimeGroup
        mWeekTimeGroup = New List(Of WeekTimeGroup)

        For i = 0 To 63
            oList(i) = New WeekTimeGroup()
            oList(i).Index = i + 1
        Next

        mWeekTimeGroup.AddRange(oList)
    End Sub

    Public ReadOnly Property Count As Integer
        Get
            Return mWeekTimeGroup.Count
        End Get
    End Property

    Public ReadOnly Property Item(ByVal Index As Integer) As WeekTimeGroup
        Get
            Return mWeekTimeGroup.Item(Index)
        End Get
    End Property

End Class


''' <summary>
''' 表示一个完整时段，一个时段里包含7天
''' </summary>
''' <remarks></remarks>
Public Class WeekTimeGroup

    Protected mDay As List(Of DayTimeGroup)
    Protected mIndex As Integer

    ''' <summary>
    ''' 星期一
    ''' </summary>
    Public Const Monday As Integer = vbMonday
    ''' <summary>
    ''' 星期二
    ''' </summary>
    Public Const Tuesday As Integer = vbTuesday
    ''' <summary>
    ''' 星期三
    ''' </summary>
    Public Const Wednesday As Integer = vbWednesday
    ''' <summary>
    ''' 星期四
    ''' </summary>
    Public Const Thursday As Integer = vbThursday
    ''' <summary>
    ''' 星期五
    ''' </summary>
    Public Const Friday As Integer = vbFriday
    ''' <summary>
    ''' 星期六
    ''' </summary>
    Public Const Saturday As Integer = vbSaturday
    ''' <summary>
    ''' 星期日
    ''' </summary>
    Public Const Sunday As Integer = vbSunday


    Public Sub New()
        mDay = New List(Of DayTimeGroup)
        Dim oList(6) As DayTimeGroup

        For i As Integer = 0 To 6
            oList(i) = New DayTimeGroup()
        Next

        mDay.AddRange(oList)
    End Sub

    Public ReadOnly Property Count As Integer
        Get
            Return mDay.Count
        End Get
    End Property

    Public ReadOnly Property Item(ByVal Index As Integer) As DayTimeGroup
        Get
            Return mDay.Item(GetWeekdayIndex(Index))
        End Get
    End Property

    Public Property Index As Integer
        Get
            Return mIndex
        End Get
        Set(ByVal value As Integer)
            mIndex = value
        End Set
    End Property



    Public Overridable ReadOnly Property DataLen As Integer
        Get

            Return 224

        End Get
    End Property

    ''' <summary>
    ''' 把时段数据转换为字节数组
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overridable Function GetBytes() As Byte()
        Return GetBytes(vbMonday)
    End Function

    ''' <summary>
    ''' 把时段数据转换为字节数组
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetBytes(ByVal iFistWeekIndex As Integer) As Byte()
        Dim bList As List(Of Byte) = New List(Of Byte)
        Dim iIndexList() As Integer, i As Integer

        '确定周时序
        iIndexList = GetWeekList(iFistWeekIndex)
        '开始转换
        For i = 0 To 6
            bList.AddRange(mDay.Item(iIndexList(i)).GetBytes())
        Next

        Return bList.ToArray()

    End Function


    ''' <summary>
    ''' 把字节数组转换为时段数据
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub SetBytes(ByVal bData() As Byte)
        SetBytes(vbMonday, bData)
    End Sub
    ''' <summary>
    ''' 把字节数组转换为时段数据
    ''' </summary>
    ''' <param name="bData"></param>
    ''' <remarks></remarks>
    Public Overridable Sub SetBytes(ByVal iFistWeekIndex As Integer, ByVal bData() As Byte)
        Dim bTmp() As Byte, iIndex As Integer = 0
        Dim iLen As Integer
        Dim iIndexList() As Integer, i As Integer
        '确定周时序
        iIndexList = GetWeekList(iFistWeekIndex)
        '开始转换
        iLen = mDay(0).Count * 4
        ReDim bTmp(iLen - 1)
        For i = 0 To 6
            If (iIndex + iLen) > bData.Length Then Exit For

            Array.Copy(bData, iIndex, bTmp, 0, iLen)
            iIndex += iLen
            mDay.Item(iIndexList(i)).SetBytes(bTmp)
        Next
    End Sub

    Protected Function GetWeekList(ByVal iFistWeekIndex As Integer) As Integer()
        Dim iList(6) As Integer, i As Integer, Index As Integer
        Dim lBeginIndex As Integer = GetWeekdayIndex(iFistWeekIndex)


        For i = 0 To 6 - lBeginIndex
            iList(i) = lBeginIndex + i
        Next

        If Not lBeginIndex = 0 Then
            Index = 0
            For i = i To 6
                iList(i) = 0 + Index
                Index += 1
            Next
        End If

        Return iList
    End Function

    Private Function GetWeekdayIndex(ByVal iWeekday As Integer) As Integer
        Dim iWeekList(10) As Integer

        iWeekList(vbMonday) = 0 '星期一
        iWeekList(vbTuesday) = 1 '星期二
        iWeekList(vbWednesday) = 2 '星期三
        iWeekList(vbThursday) = 3 '星期四
        iWeekList(vbFriday) = 4 '星期五
        iWeekList(vbSaturday) = 5 '星期六
        iWeekList(vbSunday) = 6 ' 星期日

        Return iWeekList(iWeekday)
    End Function

    ''' <summary>
    ''' 从oSource的实例拷贝数据到本实例
    ''' </summary>
    ''' <param name="oSource"></param>
    ''' <remarks></remarks>
    Sub CopyFrom(ByVal oSource As WeekTimeGroup)
        Dim iIndex As Integer
        For i As Integer = 1 To 7
            iIndex = GetWeekdayIndex(i)
            For j As Integer = 0 To mDay.Item(iIndex).Count - 1
                mDay.Item(iIndex).Item(j).SetBytes(oSource.Item(i).Item(j).GetBytes)
            Next
        Next
    End Sub




End Class


''' <summary>
''' 表示一天的时段 ,一天可以包含多个时段
''' </summary>
''' <remarks></remarks>
Public Class DayTimeGroup
    Protected mSegment As List(Of TimeSegment)

    Public Sub New()
        mSegment = New List(Of TimeSegment)
        SetSegmentCount(8)
    End Sub

    ''' <summary>
    ''' 设置每日时段中的时间片段数量
    ''' </summary>
    ''' <remarks></remarks>
    Public Overridable Sub SetSegmentCount(ByVal iTimeSegmentCount As Byte)
        Dim oList(iTimeSegmentCount - 1) As TimeSegment

        For i As Integer = 0 To iTimeSegmentCount - 1
            oList(i) = New TimeSegment
        Next
        mSegment.Clear()
        mSegment.AddRange(oList)
    End Sub

    Public ReadOnly Property Count As Integer
        Get
            Return mSegment.Count
        End Get
    End Property

    Public ReadOnly Property Item(ByVal Index As Integer) As TimeSegment
        Get
            Return mSegment.Item(Index)
        End Get
    End Property

    ''' <summary>
    ''' 把时段数据转换为字节数组
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetBytes() As Byte()
        Dim bList As List(Of Byte) = New List(Of Byte)
        For Each oSegment As TimeSegment In mSegment
            bList.AddRange(oSegment.GetBytes())
        Next

        Return bList.ToArray()

    End Function

    ''' <summary>
    ''' 把字节数组转换为时段数据
    ''' </summary>
    ''' <param name="bData"></param>
    ''' <remarks></remarks>
    Public Overridable Sub SetBytes(ByVal bData() As Byte)
        Dim bTmp(3) As Byte, iIndex As Integer = 0
        Dim i As Integer

        For i = 0 To mSegment.Count - 1
            If (iIndex + 4) > bData.Length Then Exit For

            Array.Copy(bData, iIndex, bTmp, 0, 4)
            iIndex += 4

            mSegment.Item(i).SetBytes(bTmp)
        Next
    End Sub
End Class

''' <summary>
''' 表示一个时段，开始时间和结束时间
''' </summary>
''' <remarks></remarks>
Public Class TimeSegment
    Private mBeginTime As Date
    Private mEndTime As Date

    Public Sub New()
        mBeginTime = Date.MinValue
        mEndTime = Date.MinValue
    End Sub

    Public Property BeginTime As Date
        Get
            Return mBeginTime
        End Get
        Set(ByVal value As Date)
            mBeginTime = TimeSerial(value.Hour, value.Minute, 0)
        End Set
    End Property

    Public Property EndTime As Date
        Get
            Return mEndTime
        End Get
        Set(ByVal value As Date)
            mEndTime = TimeSerial(value.Hour, value.Minute, 0)
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return Format(mBeginTime, "HH:mm") & " - " & Format(mEndTime, "HH:mm")
    End Function

    Public Overridable Function GetBytes() As Byte()
        Dim bData(3) As Byte
        If mBeginTime = Date.MinValue And mEndTime = Date.MinValue Then
            For i As Integer = 0 To bData.Length - 1
                bData(i) = 255
            Next
            Return bData
        Else
            bData(0) = BeginTime.Hour
            bData(1) = BeginTime.Minute
            bData(2) = EndTime.Hour
            bData(3) = EndTime.Minute
            Return ModTool.ByteToBCDNUM(bData)
        End If


    End Function

    Public Overridable Sub SetBytes(ByVal bData() As Byte)
        Dim bSave As Boolean
        bSave = True
        For i As Integer = 0 To bData.Length - 1
            If bData(i) > 89 Then
                bSave = False
                Exit For
            End If
        Next

        If bSave Then
            Dim bTmp() As Byte = ModTool.BCDToByte(bData)
            mBeginTime = TimeSerial(bTmp(0), bTmp(1), 0)
            mEndTime = TimeSerial(bTmp(2), bTmp(3), 0)
        Else
            mBeginTime = Date.MinValue
            mEndTime = Date.MinValue
        End If
    End Sub

End Class
